/**
 * Title AudioFilesServiceImpl
 * Package com.sba.manage.service.impl
 * Copyright 2024 www.hundsun.com All Rights Reserved.
 *
 * @author gd
 * @date 2024/9/10 12:32 AM
 * @version V5.1.2
 */
package com.ruoyi.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.manage.db.dao.SbaAudioFilesMapper;
import com.ruoyi.manage.db.dao.SbaAudioFilesSmpMapper;
import com.ruoyi.manage.db.dao.SbaUserFavoriteAudioMapper;
import com.ruoyi.manage.db.dao.SbaUserFavoriteFolderMapper;
import com.ruoyi.manage.db.model.*;
import com.ruoyi.manage.service.IAudioFiles;
import com.ruoyi.manage.db.dao.*;
import com.ruoyi.manage.db.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 音频文件管理
 * ClassName AudioFilesServiceImpl
 *
 * @author gd
 * @date 2024/9/10 12:32 AM
 */
@Service
public class AudioFilesServiceImpl implements IAudioFiles {

    @Autowired
    private SbaAudioFilesMapper sbaAudioFilesMapper;

    @Autowired
    private SbaAudioFilesSmpMapper sbaAudioFilesSmpMapper;

    @Autowired
    private SbaUserFavoriteAudioMapper sbaUserFavoriteAudioMapper;

    @Autowired
    private SbaUserFavoriteFolderMapper sbaUserFavoriteFolderMapper;

    /**
     * 根据类别获取音频列表
     *
     * @param categoryId 类别
     * @return 音频列表
     */
    @Override
    public PageInfo<SbaAudioFilesSmp> queryListByCategoryId(String categoryId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SbaAudioFilesSmp> sbaAudios = sbaAudioFilesSmpMapper.selectByType(categoryId);
        return new PageInfo<>(sbaAudios);
    }

    /**
     * 根据音频文件 id 查询文件
     *
     * @param audioFileId 音频文件 id
     * @return 音频文件信息
     */
    @Override
    public SbaAudioFiles queryListByAudioFileId(String audioFileId) {
        return sbaAudioFilesMapper.selectByPrimaryKey(audioFileId);
    }

    /**
     * 收藏文件
     *
     * @param audioFileId
     */
    @Override
    public void favoritefile(String audioFileId, String userID) {
        String defaultFavId = "000";
        //查询默认收藏夹
        SbaUserFavoriteFolderExample example = new SbaUserFavoriteFolderExample();
        example.createCriteria()
                .andFavIdEqualTo(defaultFavId)
                .andUserIdEqualTo(userID);
        long i = sbaUserFavoriteFolderMapper.countByExample(example);
        if (i <= 0) {
            //创建文件夹
            SbaUserFavoriteFolder folder = new SbaUserFavoriteFolder();
            folder.setFavId(defaultFavId);
            folder.setUserId(userID);
            folder.setFavName("默认收藏夹");
            folder.setFavCount(1);
            folder.setCreateTime(new Date());
            sbaUserFavoriteFolderMapper.insert(folder);
        }
        //插入收藏表
        SbaUserFavoriteAudioExample audioExample = new SbaUserFavoriteAudioExample();
        audioExample.createCriteria().andAudioIdEqualTo(audioFileId)
                .andFavFolderIdEqualTo(defaultFavId)
                .andUserIdEqualTo(userID);
        i = sbaUserFavoriteAudioMapper.countByExample(audioExample);
        if(i <= 0) {
            SbaUserFavoriteAudio sufa = new SbaUserFavoriteAudio();
            sufa.setAudioId(audioFileId);
            sufa.setFavFolderId(defaultFavId);
            sufa.setUserId(userID);
            sufa.setSortNum(0);
            sufa.setCreateTime(new Date());
            sbaUserFavoriteAudioMapper.insert(sufa);
        }

        //音频喜欢次数加 1
        SbaAudioFiles saftemp = sbaAudioFilesMapper.selectByPrimaryKey(audioFileId);
        if(null != saftemp) {
            SbaAudioFiles saf = new SbaAudioFiles();
            saf.setAudioFileId(audioFileId);
            saf.setLikes(saftemp.getLikes() + 1);
            sbaAudioFilesMapper.updateByPrimaryKeySelective(saf);
        }
    }


}